<?php
// 连接数据库
// 查询账号数据
// 引用数据库连接文件
require_once 'databaseConnection.php';

try {
    $conn = connectToDatabase();
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 查询数据库中的账号数据
    $sql = "SELECT * FROM account";
    $stmt = $conn->prepare($sql);
    $stmt->execute();

    // 生成账号列表
    if ($stmt->rowCount() > 0) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo "<tr>";
            echo "<td>" . $row["account_id"] . "</td>";
            echo "<td>" . $row["username"] . "</td>";
            echo "<td>" . $row["phone"] . "</td>";
            echo "<td>" . $row["password"] . "</td>";
            echo "<td>" . $row["role"] . "</td>";
            echo "<td>" . $row["createdat"] . "</td>";
            echo "<td>" . $row["lastloginat"] . "</td>";
            echo "<td>" . $row["logincount"] . "</td>";
            echo "<td>" . $row["status"] . "</td>";
            echo "<td><button class='btn btn-primary editBtn' data-accountid='" . $row["account_id"] . "'>编辑</button></td>";
            echo "<td><button class='btn btn-danger deleteBtn' data-accountid='" . $row["account_id"] . "'>删除</button></td>";
            echo "</tr>";
        }
    } else {
        echo "0 结果";
    }
} catch (PDOException $e) {
    die("连接失败: " . $e->getMessage());
}

$conn = null;
